﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace Components
{
    public class Timer : WorkflowTrigger
    {
        public Timer():base()
        {
            this.ActivityType = WorkflowActivityType.Trigger;
            this.ComponentName = "Timer";
            this.Description = "Recursive task";

            this._Instruction=new TimerInstruction();
            this._Output=new TimerOutput();
        }

        #region Overrides of WorkflowComponentBase

        private TimerInstruction _Instruction;
        public override InstructionBase Instruction
        {
            get
            {
                return this._Instruction;
            }
            set
            {
                _Instruction =(TimerInstruction) value;
            }
        }

        private TimerOutput _Output;
        public override ResultBase Output
        {
            get
            {
                return _Output;
            }
            set
            {
                _Output = (TimerOutput) value;
            }
        }
        #endregion

        #region Overrides of WorkflowTrigger

        public override TriggerType TriggerType
        {
            get { return TriggerType.Timer; }
        }

        public override bool CanRun()
        {
            if (!this._Instruction.IsActive)
                return false;
            if (this._Instruction.LastRunTime.HasValue)
            {
                if (this._Instruction.LastRunTime.Value.Add(this._Instruction.Span) < DateTime.Now)
                    return true;
                else
                    return false;
            }
            else
                return true;
        }

        public override void UpdateNextRun()
        {
            this._Instruction.LastRunTime = DateTime.Now;
            this._Instruction.NextRunTime =
                this._Instruction.LastRunTime.Value.Add(this._Instruction.Span);
        }

        #endregion
    }
}
